import { passwordUpdate } from "../../server/user";
import { verification } from "../../utils/verification";
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPassword:"",
    errorOldPassword: "",
    password1: "",
    errorPassword1: "",
    password2: "",
    errorPassword2: "",
    errorOldPasswordRegister: false,
    errorPassword1Register: false,
    errorPassword2Register: false,
  },
  inputPassword() {
    // console.log(e.detail.value);
    // this.setData({
    //   password1: e.detail.value
    // })
    if (!verification.JudgmentNull(this.data.oldPassword.trim())) {
      this.setData({
        errorOldPassword: "密码不能为空",
        errorOldPasswordRegister: false
      })
    } else if (!verification.checkPassword(this.data.oldPassword.trim(), 6)) {
      this.setData({
        errorOldPassword: "密码至少六位",
        errorOldPasswordRegister: false
      })
    } else {
      this.setData({
        errorOldPassword: "",
        errorOldPasswordRegister: true
      })
    }
  },
  inputPassword1() {
    if (!verification.JudgmentNull(this.data.password1.trim())) {
      this.setData({
        errorPassword1: "密码不能为空",
        errorPassword1Register: false
      })
    } else if (!verification.checkPassword(this.data.password1.trim(), 6)) {
      this.setData({
        errorPassword1: "密码至少六位",
        errorPassword1Register: false
      })
    } else {
      this.setData({
        errorPassword1: "",
        errorPassword1Register: true
      })
    }
  },
  inputPassword2() {
    // console.log(e.detail.value);
    // this.setData({
    //   password2: e.detail.value
    // })
    if (!verification.JudgmentNull(this.data.password2.trim())) {
      this.setData({
        errorPassword2: "密码不能为空",
        errorPassword2Register: false
      })
    } else if (!verification.checkPassword(this.data.password2.trim(), 6)) {
      this.setData({
        errorPassword2: "密码至少六位",
        errorPassword2Register: false
      })
    } else if (this.data.password1 != this.data.password2) {
      this.setData({
        errorPassword2: "两次输入的密码不一致",
        errorPassword2Register: false
      })
    } else {
      this.setData({
        errorPassword2: "",
        errorPassword2Register: true
      })
    }
  },
  updatePassword(){
    if(this.data.errorOldPasswordRegister && this.data.errorPassword1Register && this.data.errorPassword2Register){
      const  data = {
        old_password: this.data.oldPassword,
        password:this.data.password1,
        password_confirmation:this.data.password2,
      }
      passwordUpdate(data).then( () => {
        Toast({
          message: "修改成功",
          type: 'success',
          onClose: () => {
            wx.navigateBack({
              delta: 1
            })
          }
        });
      })
    }else{
      this.inputPassword()//判断原密码格式
      this.inputPassword1()//判断新密码格式
      this.inputPassword2()//判断两次密码输入是否一致
    }
  }
})